package com.kejiguan.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.kejiguan.ui.R;
import com.kejiguan.ui.model.NeItem;

/**
 * Created by apple on 16/3/13.
 */
public class AddNeItemDialog extends Dialog implements View.OnClickListener{

    private View mDialogView;
    private EditText idInput,nameInput,ipInput,macInput;
    private Button confirmBtn,cancelBtn;

    private String neId,neName,neIp,mac;
    private OnNewNeItemAddListener onNewNeItemAddListener;

    public AddNeItemDialog(Context context) {
        super(context);
        init(context);
    }

    public AddNeItemDialog(Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected AddNeItemDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
        setCanceledOnTouchOutside(true);
    }

    private void init(Context context) {

        mDialogView = View.inflate(context, R.layout.dialog_add_device_layout, null);
        idInput = (EditText) mDialogView.findViewById(R.id.id_input);
        nameInput = (EditText) mDialogView.findViewById(R.id.device_name_input);
        ipInput = (EditText) mDialogView.findViewById(R.id.device_ip_input);
        macInput = (EditText) mDialogView.findViewById(R.id.device_mac_input);

        confirmBtn=(Button)mDialogView.findViewById(R.id.add_device_confirm_btn);
        cancelBtn=(Button)mDialogView.findViewById(R.id.add_device_cancel_btn);
        confirmBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);

        setContentView(mDialogView);
        setCanceledOnTouchOutside(true);
    }

    public void setOnNewNeItemAddListener(OnNewNeItemAddListener onNewNeItemAddListener) {
        this.onNewNeItemAddListener = onNewNeItemAddListener;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.add_device_confirm_btn:

                if (null!=onNewNeItemAddListener)
                {
                    neId=idInput.getText().toString().trim();
                    if (TextUtils.isEmpty(neId))
                    {
                        Toast.makeText(getContext(),"设备ID不能为空",Toast.LENGTH_SHORT).show();
                        break;
                    }
                    neName=nameInput.getText().toString().trim();
                    if (TextUtils.isEmpty(neName))
                    {
                        Toast.makeText(getContext(),"设备名称不能为空",Toast.LENGTH_SHORT).show();
                        break;
                    }
                    neIp=ipInput.getText().toString().trim();
                    if (TextUtils.isEmpty(neIp))
                    {
                        Toast.makeText(getContext(),"设备IP不能为空",Toast.LENGTH_SHORT).show();
                        break;
                    }
                    mac=macInput.getText().toString().trim();
                    if (TextUtils.isEmpty(mac))
                    {
                        Toast.makeText(getContext(),"设备Mac地址不能为空",Toast.LENGTH_SHORT).show();
                        break;
                    }
                    NeItem newNeItem=new NeItem();
                    newNeItem.setWpaId("");
                    newNeItem.setId(neId);
                    newNeItem.setNeName(neName);
                    newNeItem.setMac(mac);
                    newNeItem.setNeIp(neIp);
                    newNeItem.setNeType("Dell");
                    newNeItem.setNote(neName);
                    newNeItem.setNePort("8086");
                    newNeItem.setBroadcastIp("255.255.255.255");

                    onNewNeItemAddListener.onNewNeItemAdd(newNeItem);
                    dismiss();
                }

                break;
            case R.id.add_device_cancel_btn:
                dismiss();
                break;
        }
    }

    public interface OnNewNeItemAddListener
    {
        public void onNewNeItemAdd(NeItem neItem);
    }
}
